package com.china08.yxyapi.api.manage.tec.teaching.lesson.cloud.ques;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLessonItem;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLessonItemQues;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonItemQuesRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonItemRepository;

/**
 * 
 * @annotation: 云课程--插入课时对应试题ID
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class QuesCloudLessonUpdItemController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsCloudLessonItemQuesRepository yxyLessonsCloudLessonItemQuesRepository;

    @Autowired
    YxyLessonsCloudLessonItemRepository itemRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lesson/cloud/ques/item/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String itemId = reqModel.itemId;
        List<String> quesIdList = reqModel.quesIdList;
        List<YxyLessonsCloudLessonItemQues> lessonItems = new ArrayList<YxyLessonsCloudLessonItemQues>();
        List<YxyLessonsCloudLessonItemQues> lessonItemId = yxyLessonsCloudLessonItemQuesRepository
                .findByLessonItemId(itemId);
        YxyLessonsCloudLessonItem cloudLessonItem = itemRepository.findById(itemId);
        if (cloudLessonItem != null) {
            cloudLessonItem.setStatus(0);
            itemRepository.save(cloudLessonItem);
        }
        int i = 1;
        if (lessonItemId != null) {
            yxyLessonsCloudLessonItemQuesRepository.delete(lessonItemId);
            if (quesIdList != null && quesIdList.size() > 0) {
                for (String string : quesIdList) {
                    YxyLessonsCloudLessonItemQues qLessonItemQues = new YxyLessonsCloudLessonItemQues();
                    qLessonItemQues.setLessonItemId(itemId);
                    qLessonItemQues.setQuesId(string);
                    qLessonItemQues.setOrderNum(i);
                    lessonItems.add(qLessonItemQues);
                    i++;
                }
            }
            yxyLessonsCloudLessonItemQuesRepository.save(lessonItems);
        } else {
            if (quesIdList != null && quesIdList.size() > 0) {
                for (String string : quesIdList) {
                    YxyLessonsCloudLessonItemQues qLessonItemQues = new YxyLessonsCloudLessonItemQues();
                    qLessonItemQues.setLessonItemId(itemId);
                    qLessonItemQues.setQuesId(string);
                    qLessonItemQues.setOrderNum(i);
                    lessonItems.add(qLessonItemQues);
                    i++;
                }
            }
            yxyLessonsCloudLessonItemQuesRepository.save(lessonItems);
        }

        RespModel model = new RespModel();
        model.setMessage("插入试题成功");
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 课时ID
        public String itemId;
        // 试题ID
        public List<String> quesIdList;
    }

    protected static class RespModel extends YxyApiRespModel {
    }
}
